package com.yimils.core.goods.serivce.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yimils.core.goods.dto.ChestAddReq;
import com.yimils.core.goods.dto.ChestUpdateReq;
import com.yimils.core.goods.entity.ChestEntity;
import com.yimils.core.goods.mapper.ChestMapper;
import com.yimils.core.goods.serivce.ChestService;
import com.yimils.core.goods.vo.ChestRes;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service("chestService")
public class ChestServiceImpl extends ServiceImpl<ChestMapper, ChestEntity> implements ChestService {
    @Override
    public Boolean saveChest(ChestAddReq chestAddReq) {
        ChestEntity chestEntity = new ChestEntity();
        chestEntity.setChestName(chestAddReq.getChestName());
        chestEntity.setHigh(chestAddReq.getHigh());
        chestEntity.setLength(chestAddReq.getLength());
        chestEntity.setWide(chestAddReq.getWide());
        chestEntity.setState(Boolean.FALSE);
        chestEntity.setCreateTime(LocalDateTime.now());
        chestEntity.setUpdateTime(LocalDateTime.now());
        return save(chestEntity);
    }

    @Override
    public Boolean updateChest(ChestUpdateReq chestUpdateReq) {
        QueryWrapper<ChestEntity> qw = new QueryWrapper<>();
        qw.lambda().eq(ChestEntity::getId, chestUpdateReq.getChestId()).eq(ChestEntity::getState, Boolean.FALSE);
        ChestEntity entity = getOne(qw);
        entity.setChestName(chestUpdateReq.getChestName());
        entity.setLength(chestUpdateReq.getLength());
        entity.setWide(chestUpdateReq.getWide());
        entity.setHigh(chestUpdateReq.getHigh());
        entity.setUpdateTime(LocalDateTime.now());
        return saveOrUpdate(entity);
    }

    @Override
    public Boolean deleteChest(List<Long> chestIds) {
        QueryWrapper<ChestEntity> qw = new QueryWrapper<>();
        qw.lambda().in(ChestEntity::getId, chestIds).eq(ChestEntity::getState, Boolean.FALSE);
        List<ChestEntity> chestEntities = list(qw);
        if (CollectionUtils.isEmpty(chestEntities)) {
            return Boolean.FALSE;
        }
        chestEntities.forEach(o -> {
            o.setUpdateTime(LocalDateTime.now());
            o.setState(Boolean.TRUE);
        });
        return saveOrUpdateBatch(chestEntities);
    }

    @Override
    public List<ChestRes> listChest() {
        QueryWrapper<ChestEntity> qw = new QueryWrapper<>();
        qw.lambda().eq(ChestEntity::getState, Boolean.FALSE);

        List<ChestEntity> chestEntities = list(qw);
        return chestEntities.stream().map(o -> {
            ChestRes chestRes = new ChestRes();
            chestRes.setChestName(o.getChestName());
            chestRes.setChestId(o.getId());
            chestRes.setHigh(o.getHigh());
            chestRes.setLength(o.getLength());
            chestRes.setWide(o.getWide());
            return chestRes;
        }).collect(Collectors.toList());
    }

    @Override
    public List<ChestEntity> listChest(List<Long> chestIds) {
        QueryWrapper<ChestEntity> qw = new QueryWrapper<>();
        qw.lambda().eq(ChestEntity::getId, chestIds).eq(ChestEntity::getState, Boolean.FALSE);
        return list(qw);
    }
}
